<template>
    <el-container>
        <el-header class="header-tabs">
            <el-tabs type="card" v-model="groupId" @tab-change="tabChange">
                <el-tab-pane label="报价单列表" name="1"></el-tab-pane>
                <el-tab-pane label="报价单商品明细" name="2"></el-tab-pane>
            </el-tabs>
        </el-header>
        <el-header>
            <div class="left-panel">
                <el-button type="primary" icon="el-icon-plus" v-auth="'offer.add'" @click="add"></el-button>
                <el-button type="danger" plain icon="el-icon-delete" v-auth="'offer.delete'" :disabled="selection.length == 0" @click="batch_del"></el-button>
                <el-button type="warning" plain v-auth="'offer.check'" :disabled="selection.length == 0" @click="batch_check" >审核</el-button>
                <el-popconfirm title="确定反审核吗?" @confirm="batch_uncheck">
                    <template #reference>
				        <el-button type="info" plain v-auth="'offer.uncheck'" :disabled="selection.length == 0" >反审核</el-button>
                    </template>
                </el-popconfirm>
                <sc-print-button :auth="'offer.print'" :selection="selection" v-if="groupId == 1"></sc-print-button>
            </div>
            <div class="right-panel">
                <div class="right-panel-search">
                    <el-input v-model="search.contract_code" placeholder="请输入报价编号" clearable @keyup.enter="upsearch"></el-input>
                    <el-input v-model="search.customer_name" placeholder="请输入客户关键词" clearable @keyup.enter="upsearch"></el-input>
                    <el-input v-model="search.goods_name" placeholder="请输入商品关键词" clearable @keyup.enter="upsearch" v-if="groupId == '2'"></el-input>
                    <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                    <el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
                </div>
            </div>
        </el-header>
        <el-main class="nopadding" v-if="groupId == '1'">
            <scTable ref="table" row-key="id" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort remoteFilter border :params="params">
                <el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
                <el-table-column type="index" label="序号" fixed align="center" width="50"></el-table-column>
                <el-table-column label="报价编号" prop="contract_code" align="center"></el-table-column>
                <el-table-column label="客户" prop="customer.customer_name" align="center"></el-table-column>
                <el-table-column label="备注" prop="remark" align="center"></el-table-column>
                <el-table-column label="开单日期" prop="open_single" align="center"></el-table-column>
                <el-table-column label="报价交期" prop="deliver_date" align="center"></el-table-column>
                <el-table-column label="报价金额" prop="contract_price" align="center"></el-table-column>
                <el-table-column label="销售单" prop="is_offer" align="center">
                    <template #default="scope">
                        <el-tag :type="scope.row.is_offer == 1 ? 'success' : 'danger'">{{ scope.row.is_offer == 1 ? '已生成' : '未生成' }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="审核状态" prop="check_status" align="center">
                    <template #default="scope">
                        <el-tag v-if="scope.row.check_status == 3" type="warning">待审核</el-tag>
                        <el-tag v-if="scope.row.check_status == 1" type="success">已通过</el-tag>
                        <!-- <el-tag v-if="scope.row.check_status == 2" type="danger">驳回<span v-if="scope.row.check_remark">：</span>{{ scope.row.check_remark }}</el-tag> -->
                        <el-popover placement="top-start" :width="200" trigger="hover" :content="scope.row.check_remark">
                            <template #reference>
                                <el-tag v-if="scope.row.check_status == 2" type="danger">驳回</el-tag>
                            </template>
                        </el-popover>
                    </template>
                </el-table-column>
                <el-table-column label="操作" fixed="right" align="center" width="170">
                    <template #default="scope">
                        <el-button-group>
                            <el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)" v-auth="'offer.show'">查看</el-button>
                            <el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)" v-auth="'offer.edit'" v-if="scope.row.is_offer == 0">编辑</el-button>
                            <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
                                <template #reference>
                                    <el-button text type="danger" size="small" v-auth="'offer.delete'" v-if="scope.row.is_offer == 0">删除</el-button>
                                </template>
                            </el-popconfirm>
                            <el-popconfirm title="确定转报价单吗？" @confirm="table_transfer(scope.row)">
                                <template #reference>
                                    <el-button text type="warning" size="small" v-auth="'offer.transfer'" v-if="scope.row.is_offer == 0 && scope.row.check_status == 1">转销售单</el-button>
                                </template>
                            </el-popconfirm>
                        </el-button-group>
                    </template>
                </el-table-column>
            </scTable>
        </el-main>
        <el-main class="nopadding" v-if="groupId == '2'">
            <scTable ref="table" row-key="id" :apiObj="shopObj" @selection-change="selectionChange" stripe remoteSort remoteFilter border :params="params">
                <el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
                <el-table-column type="index" label="序号" fixed align="center" width="50"></el-table-column>
                <el-table-column label="报价单号" prop="contract_code" align="center"></el-table-column>
                <el-table-column label="客户名称" prop="customer_name" align="center"></el-table-column>
                <el-table-column label="商品名称" prop="goods.goods_name" align="center"></el-table-column>
                <el-table-column label="商品编码" prop="goods.goods_code" align="center"></el-table-column>
                <el-table-column label="规格型号" prop="goods.goods_spec" align="center"></el-table-column>
                <el-table-column label="商品单位" prop="goods.unit.unit_name" align="center"></el-table-column>
                <el-table-column label="商品分类" prop="goods.cate.cate_name" align="center"></el-table-column>
                <el-table-column label="数量" prop="sale_num" align="center"></el-table-column>
                <el-table-column label="开单日期" prop="open_single" align="center"></el-table-column>
                <el-table-column label="报价交期" prop="deliver_date" align="center"></el-table-column>
            </scTable>
        </el-main>
    </el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false"></save-dialog>
	<check-status v-if="dialog.check" ref="checkDialog" @success="handleSuccess" @closed="dialog.check = false"></check-status>
</template>

<script>
import saveDialog from './save'
import CheckStatus from './checkStatus.vue'

export default {
	name: 'offer列表',
	components: {
		saveDialog,CheckStatus
	},
	data() {
		return {
			dialog: {
				save: false,
				check: false,
			},
			apiObj: this.$API.contract.contract.list,
            shopObj: this.$API.contract.contract.detailList,
			selection: [],
			search: {
				contract_code: null,
                customer_name:null,
                goods_name: null,
			},
			params:{
				is_offer:0
			},
            groupId: "1",
		}
	},
	mounted() {
	},
	methods: {
		//添加
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
		},
		//编辑
		table_edit(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		//查看
		table_show(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('show').setData(row)
			})
		},
        // 审核
		batch_check() {
            var ids = [];
			let go = true
			this.selection.forEach((item) => {
				if (item.check_status != 3) {
					go = false
				} else {
					ids.push(item.id)
				}
			})
			if (!go) {
				this.$message.error("请选择未审核的订单")
				return
			}
			this.dialog.check = true
			this.$nextTick(() => {
				this.$refs.checkDialog.open('edit').setData(ids)
			})
		},
		//反审核
		async batch_uncheck() {
            var ids = [];
			let go = true
			this.selection.forEach((item) => {
				if (item.check_status == 3 || item.is_offer == 1) {
					go = false
				} else {
					ids.push(item.id)
				}
			})
            if (!go) {
				this.$message.error("订单未审核或已生成销售订单，无法反审核")
				return
			}
            var reqData = {id: ids, check_status:3}
			var res = await this.$API.contract.contract.offerCheck.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
				this.$message.success("操作成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
        async table_transfer(row) {
			var reqData = { id: row.id }
			var res = await this.$API.contract.contract.transfer.post(reqData);
			if (res.code == 1) {
                this.$refs.table.refresh()
				this.$message.success("操作成功")
			} else {
				this.$alert(res.message, "提示", { type: 'error' })
			}
		},
		//删除
		async table_del(row, index) {
			var reqData = { id: row.id }
			var res = await this.$API.contract.contract.delete.post(reqData);
			if (res.code == 1) {
				this.$refs.table.tableData.splice(index, 1);
                this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", { type: 'error' })
			}
		},
		async table_del_all(delete_ids) {
			var reqData = { id: delete_ids }
			var res = await this.$API.contract.contract.delete.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", { type: 'error' })
			}
		},
		//批量删除
		async batch_del() {
			var delete_ids = [];
			this.selection.forEach((item) => {
				delete_ids.push(item.id)
			})
			this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(() => {
				this.table_del_all(delete_ids)
			}).catch(() => {

			})
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
        refresh(){
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.$refs.table.upData(this.search)
        },
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			}
		},
	}
}
</script>

<style></style>
